(function () {
    var $page = $('.page-selldemand'),
        Dialog = require('wap_common:widget/ui/dialog/dialog.js').Dialog,
        util = require('wap_common:widget/ui/libs/util.js').util,
        dialog = new Dialog(),
        $previewBlock = $page.find('.preview-block'),
        $loadingBlock = $page.find('.loading-block'),
        $successBlock = $page.find('.success-block'),
        $failBlock = $page.find('.fail-block'),
        $timeoutBlock = $page.find('.timeout-block'),
        trade_id, sell_amount,
        data = P.data('data'),
        count = 0, interval;

    $page.find('.btn-redeem').bind('touchstart', function () {
        if ($(this).hasClass('btn-main')) {
            var amount = sell_amount = $.trim($page.find('.amount').val());

            if (parseFloat(amount) <= 0 || !/^[\d|\.]+$/.test(amount)) {
                dialog.show("赎回金额必须大于0");
                return false;
            }

            if (parseFloat(amount) > parseFloat(data['total'])) {
                dialog.show("赎回金额超过活期持有份额");
                return false;
            }

            $(this).addClass('btn-disable').removeClass('btn-main');
            $.post('/wap/redeem', {
                amount: amount,
                pid: 1
            }, function (ret, status) {
                if (ret.error_no == 0) {
                    $loadingBlock.show();
                    $previewBlock.hide();
                    trade_id = ret.data;
                    setTimeout(function () {
                        interval = setInterval(function () {
                            refresh();
                            if (count++ >= 30) {
                                clearInterval(interval);
                            }
                        }, 2000);
                    }, 3000);
                    refresh();
                } else {
                    dialog.show(ret.error_message);
                }
            }, 'json');
        }
    });
    function refresh() {
        $.get('/user/gettradestatus', {
            'serial_id': trade_id
        }, function (ret, st) {
            if (ret.error_no == 0) {
                var status = ret.data.tradeItem.status;
                if (status == 2) {
                    $loadingBlock.hide();
                    $failBlock.show();
                    clearInterval(interval);
                } else if (status == 1) {
                    $loadingBlock.hide();
                    $successBlock.show().find('.amount').text(parseFloat(ret.data.tradeItem.amount));
                    clearInterval(interval);
                } else {
                    if (count > 10) {
                        $loadingBlock.hide();
                        $timeoutBlock.show();
                        clearInterval(interval);
                    }
                }
            }
        }, 'json');
    }
    $page.find('.trade-log-btn').bind('touchstart',function() {
        location.href = '/wap/tradelog';
    });
    $page.find('.wallet-btn').bind('touchstart',function() {
        location.href = '/wap/wallet';
    });

})();